package com.guanjia.web;

import com.guanjia.entity.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guanjia.entity.vo.Image;
import com.guanjia.entity.vo.ImageCorr;
import com.guanjia.entity.vo.InvestInfoVo;
import com.guanjia.mapper.CreditinfoMapper;
import com.guanjia.mapper.UserMapper;
import com.guanjia.service.BorrowService;
import com.guanjia.service.InvestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/invest")
public class InvestController {
    @Autowired
    private InvestService investService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CreditinfoMapper creditinfoMapper;
    @Autowired
    private BorrowService borrowService;

    @RequestMapping("/form")
    public String touziForm(@RequestParam("investid") String investid, Model model, HttpSession session){

        User user =(User) session.getAttribute("user");
        if(user==null){
            return "redirect:/login";

        }
        UserInformation uInf=userMapper.findUserInfoByUserId(user.getId());
        if(uInf.getName()==null||uInf.getName().equals("")){
        return "redirect:/userCenter/account";
        }

        model.addAttribute("invest",investid);
        model.addAttribute("userInf",uInf);
        return "/cfgj/touzi_jine";
    }

    @RequestMapping("/xiangqing")
    public String touziXq(Integer id,Model model){
        CreditInformation creditInformation = investService.findInfoById(id);
        List<InvestInfoVo> investvo = investService.findById(id);
        List<Repayment> repayments = borrowService.findRepay(id);
        List<Image> car = investService.FindCarList(id);
        List<Image> home = investService.FindHomeList(id);
        List<Image> idCard = investService.FindIdCardList(id);
        List<Image> other = investService.FindOtherList(id);
        UserInformation userInfo= userMapper.findUserNameById(id);
        Integer count = borrowService.findcountById(id);
        Integer access = borrowService.findacessById(id);
        Double summoney = borrowService.sumBorrowMoney(id);
        Integer repaycounts = borrowService.repaycount(id);
        model.addAttribute("info",creditInformation);
        model.addAttribute("invests",investvo);
        model.addAttribute("cars",car);
        model.addAttribute("homes",home);
        model.addAttribute("idCards",idCard);
        model.addAttribute("others",other);
        model.addAttribute("user",userInfo);
        model.addAttribute("counts",count);
        model.addAttribute("repaycount",repaycounts);
        model.addAttribute("acc",access);
        model.addAttribute("sum",summoney);
        model.addAttribute("repay",repayments);
        return "/cfgj/touzi_xiangqing";
    }

    @RequestMapping("/list")
    public String list(){return "cfgj/touzi_list";}

    @RequestMapping("/getlistData")
    @ResponseBody
    public Map<String,Object> touziLsit(Model model, Integer page, Integer size,String sort,String pname,Integer max,Integer min){
//         System.out.println(sort+"--");
//          System.out.println("pname:"+pname+max+min);
        Map<String,Object> map = new HashMap<>();
        IPage<CreditInformation> list = investService.findList(page,size,sort,pname,max,min);
        if (sort!=null&&sort.equals("1")){
            map.put("sort","1");
        }
        if (sort!=null&&sort.equals("2")){
            map.put("sort","2");
        }
        map.put("pname",pname);
        map.put("max",max);
        map.put("min",min);
        map.put("data",list.getRecords());
        map.put("count",list.getTotal());
        return map;
    }
    @ResponseBody
    @RequestMapping("/save")
    public Map saveOrder(HttpSession session,@RequestParam("investid") String investid,@RequestParam("money") String money){
            Integer investId=Integer.parseInt(investid);
            User user=(User) session.getAttribute("user");
            Map<String,String> map=new HashMap<>();
        CreditInformation creditInformation=creditinfoMapper.findCreditInfoById(investId);
        Double lessMoney=(100-Double.valueOf(creditInformation.getSchedule()))*creditInformation.getBorrowmoney()/100;
        if(Integer.parseInt(money)>lessMoney){
            map.put("result1","0");
            map.put("msg","您输入的金额大于本投资剩余金额，你可以投入"+lessMoney+"元");
                return map;
        }
        if(user.getBalance()<Integer.parseInt(money)){
            map.put("result1","0");
            map.put("msg","抱歉您的投资金额不足QAQ");
            return map;
        }

            Integer crowd_user_id=creditinfoMapper.findCreditUserIdById(investId);
            investService.saveOrder(investId,user.getId(),crowd_user_id,Double.valueOf(money));
        map.put("result1","1");
        map.put("msg","感谢您，投资成功");
        return map;
    }

}
